package increment1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * TaskView class is a singleton representing the task view in the TMS
 * 
 * @author Amer Alhalabi/Rolf Schmidt
 * @version 1.0
 *
 */
public class TaskView {
	
	///  the only instance of the class 
	private static TaskView s_instance = null;
	
	/// holds all Task objects (taskId, Task)
	TreeMap<String, Task> taskList;  
	
	/**
	 * getInstance is a method that returns the only instance
	 * of the class
	 * 
	 * @return TaskView only instance
	 */
	public static TaskView getInstance() {
		if (s_instance == null) {
			s_instance = new TaskView();
		}
		return s_instance;
	}
	
	/**
	 * default constructor
	 * note: it is private to prevent creating multiple instances
	 */
	private TaskView() {
		taskList = new TreeMap<String, Task>();
	}
	
	/**
	 * a getter of people and their tasks
	 * 
	 * @return a map of person Name to its Person object
	 */
	public TreeMap<String, Task> getTaskList() {
		return taskList;
	}
	/**
	 * openFile populates the taskview instance with tasks that are
	 * read from an input file
	 * 
	 * @param filename the path to the input file that contains
	 * the tasks to be populated in the system 
	 */
	public boolean openFile(String fileName) {
		
		Scanner inputStream = null;
		TreeMap<String, Person> personList = PeopleView.getInstance().getPersonList();
		taskList.clear();

		try {
			inputStream = new Scanner(new FileInputStream(fileName));
		}
		catch (FileNotFoundException e) {
			System.out.println("File " + fileName + " was not found");
			System.out.println("or could not be opened.");
			return false;
		}
		catch (NullPointerException e) {
			System.out.println("Filename is null");
			System.out.println(e.getMessage());
			return false;
		}

		while (inputStream.hasNextLine()) {
			Task task = null;
			try 
			{
				task = getTask(inputStream);
			}
			catch (Exception e) {
				inputStream.close();
				taskList.clear();
				personList.clear();
				System.out.println("invalid file content....operation aborted");
				return false;
			}
			
			String name = task.getPerson();
			String taskId = task.getId();

			if (taskList.containsKey(taskId)) {
				inputStream.close();
				taskList.clear();
				personList.clear();
				System.out.println("invalid file content - task:" + taskId + "already exists");
				System.out.println("operation aborted");
				return false;
			}
				
			
			taskList.put(taskId, task);

			if (!personList.containsKey(name)) {
				Person person = new Person();
				person.setName(name);
				personList.put(name, person);
			}
			Person person = personList.get(name);
			person.addTask(task);
		}
		inputStream.close();
		return true;
	}
	
	private Task getTask(Scanner inputStream) throws Exception {
		
		String id = inputStream.nextLine().trim();
		String title = inputStream.nextLine().trim();
		String description = inputStream.nextLine().trim();
		int duration = inputStream.nextInt();
		inputStream.nextLine();
		String deliverable = inputStream.nextLine().trim();
		String deadline = inputStream.nextLine().trim();
		String name = inputStream.nextLine().trim();

		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			cal.setTime(sdf.parse(deadline));
		}
		catch (ParseException e) {
			System.out.println("Deadline (" + deadline
					+ ") could not be converted.");
			throw e;
		}

		Task task = new Task();
		task.setId(id);
		task.setTitle(title);
		task.setDescription(description);
		task.setDuration(duration);
		task.setDeliverable(deliverable);
		task.setDeadline(cal);
		task.setPerson(name);
		return task;
	}
	
	public void printTaskList() { 
		final String H_LINE = "***************************************"; 
		
		System.out.println(H_LINE);
		System.out.println("Tasks");
		System.out.println(H_LINE);
		
		for (Map.Entry<String, Task> entry : taskList.entrySet()) {
			System.out.println(entry.getValue().toString()); 
		}
	}
}
